package com.zh.baselibrary.model;

import android.content.Context;
import android.text.TextUtils;

import com.zh.baselibrary.api.IBookBusiness;
import com.zh.baselibrary.api.IBookLoginService;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.netutils.BaseResp;
import com.zh.baselibrary.netutils.HRetrofitNetHelper;
import com.zh.baselibrary.utils.ShareManager;
import com.zh.model.CurrentTime;
import com.zh.model.EquipmentLogin;
import com.zh.model.Errata;
import com.zh.model.ResAdsParItem;
import com.zh.model.cabinet.UserInfo;
import com.zh.model.home.TokenBean;
import com.zh.model.manager.BookInfo;
import com.zh.model.manager.SearchBook;
import com.zh.model.request.ErrorInfo;
import com.zh.model.request.LoginInfo;
import com.zh.model.request.SubmitInfo;
import com.zh.model.task.home.ReportVersionBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import retrofit2.http.Query;


public class BookModel {
    private Context context;
    public BookModel(Context context){
        this.context = context;
    }
    public Observable<BaseResp<UserInfo>> login( String schoolId,  LoginInfo info){
        return  HRetrofitNetHelper.retrofit().create(IBookLoginService.class).login(schoolId,info)
                .doOnNext(new Consumer<BaseResp<UserInfo>>() {
                    @Override
                    public void accept(BaseResp<UserInfo> userInfoBaseResp) throws Exception {
                        if(userInfoBaseResp.getData()!=null)
                        BaseConstant.DEVICE_TOKEN2 = userInfoBaseResp.getData().getToken();
                    }
                });
    }
    public Observable<BaseResp<Object>> heartbeat(String equipmentCode){
        return  HRetrofitNetHelper.retrofit().create(IBookLoginService.class).heartbeat(equipmentCode);
    }

//    public Observable<BaseResp<TokenBean>> equipmentLogin(EquipmentLogin equipmentLogin){
//        return  HRetrofitNetHelper.retrofit().create(IBookLoginService.class).equipmentLogin(equipmentLogin)
//               .doOnNext(new Consumer<BaseResp<TokenBean>>() {
//                   @Override
//                   public void accept(BaseResp<TokenBean> baseResp) throws Exception {
//                       if (baseResp.getData() != null && baseResp.getData().getToken() != null) {
//                           BaseConstant.DEVICE_TOKEN =  baseResp.getData().getToken();
//                           ShareManager.setValue(context, BaseConstant.AUTHORIZATION_DEADLINE, baseResp.getData().getAuthorizationTime());
//                       }
//                   }
//               });
//    }
    public Observable<BaseResp<TokenBean>> equipmentLogin(String equipmentCode){
        return  HRetrofitNetHelper.retrofit().create(IBookLoginService.class).equipmentLogin(equipmentCode)
                .doOnNext(new Consumer<BaseResp<TokenBean>>() {
                    @Override
                    public void accept(BaseResp<TokenBean> baseResp) throws Exception {
                        if (baseResp.getData() != null && baseResp.getData().getToken() != null) {
                            BaseConstant.DEVICE_TOKEN =  baseResp.getData().getToken();
                            ShareManager.setValue(context, BaseConstant.AUTHORIZATION_DEADLINE_BOOK, baseResp.getData().getAuthorizationTime());
                        }
                    }
                });
    }
    public Observable<BaseResp<SearchBook>> queryBook(String schoolId, String name, String equipmentCode, int pageNum, int pageSize){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).queryBook(schoolId,name,equipmentCode,pageNum,pageSize);
    }
    public Observable<BaseResp<CurrentTime>> reportBaseInfo(EquipmentLogin baseInfo){
        return  HRetrofitNetHelper.retrofit().create(IBookLoginService.class).reportBaseInfo(baseInfo)
                .doOnNext(new Consumer<BaseResp<CurrentTime>>() {
                    @Override
                    public void accept(BaseResp<CurrentTime> baseResp) throws Exception {
                        if (baseResp.getData() != null && !TextUtils.isEmpty(baseResp.getData().getCurrentTime()) ) {
                            long lt = Long.valueOf(baseResp.getData().getCurrentTime());
                            BaseConstant.timeDifferent = lt - System.currentTimeMillis();
                            ShareManager.setInt(context, BaseConstant.DEVICE_VOICE, baseResp.getData().getVoiceSwitch());
                            BaseConstant.canSpeak = baseResp.getData().getVoiceSwitch() == 1;
                        }
                    }
                });
    }
    public Observable<BaseResp<List<BookInfo>>> selectBook(String schoolId,String equipmentCode,String name){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).selectBook(schoolId,equipmentCode,name);
    }
    public Observable<BaseResp<Object>> selectCount(String schoolId){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).selectCount(schoolId);
    }
    public Observable<BaseResp<Object>> error(String schoolId, ErrorInfo info){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).error(schoolId,info);
    }
    public Observable<BaseResp<List<BookInfo>>> status(String schoolId, List<String> list){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).status(schoolId,list);
    }
    public Observable<BaseResp<Object>> operate(String schoolId, SubmitInfo info){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).operate(schoolId,info);
    }
    public Observable<BaseResp<List<BookInfo>>> borrowRecord(String schoolId){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).borrowRecord(schoolId);
    }
    public Observable<BaseResp<List<ResAdsParItem>>> getBarner(String schoolId,String equipmentCode){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).getBarner(schoolId,equipmentCode);
    }
    public Observable<BaseResp<Object>> errata(Errata errata){
        return  HRetrofitNetHelper.retrofit().create(IBookBusiness.class).errata(errata);
    }
    /**
     * 上报设备版本号
     */
    public Observable<BaseResp<Object>> reportVersion(String equipmentCode, ReportVersionBean versionBean) {
        return HRetrofitNetHelper.retrofit().create(IBookBusiness.class).reportVersion(equipmentCode, versionBean);
    }

}
